#!/bin/bash

# 物业保洁系统环境检查工具
# 自动检测系统环境和依赖软件

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

echo -e "${GREEN}=====================================${NC}"
echo -e "${GREEN}物业保洁系统环境检查工具${NC}"
echo -e "${GREEN}=====================================${NC}"
echo

# 检查函数
check_command() {
    local cmd=$1
    local name=$2
    local install_url=$3
    
    if command -v "$cmd" &> /dev/null; then
        echo -e "${GREEN}✅ $name 已安装${NC}"
        $cmd --version | head -n 1
    else
        echo -e "${RED}❌ $name 未安装${NC}"
        echo -e "${YELLOW}安装地址: $install_url${NC}"
    fi
    echo
}

# 1. 检查Docker
echo -e "${BLUE}[1] 检查Docker安装...${NC}"
check_command "docker" "Docker" "https://docs.docker.com/engine/install/"

# 2. 检查Docker Compose
echo -e "${BLUE}[2] 检查Docker Compose安装...${NC}"
check_command "docker-compose" "Docker Compose" "https://docs.docker.com/compose/install/"

# 3. 检查Java
echo -e "${BLUE}[3] 检查Java安装...${NC}"
check_command "java" "Java" "https://adoptium.net/"

# 4. 检查Node.js
echo -e "${BLUE}[4] 检查Node.js安装...${NC}"
check_command "node" "Node.js" "https://nodejs.org/"

# 5. 检查npm
echo -e "${BLUE}[5] 检查npm安装...${NC}"
check_command "npm" "npm" "https://nodejs.org/"

# 6. 检查Git
echo -e "${BLUE}[6] 检查Git安装...${NC}"
check_command "git" "Git" "https://git-scm.com/downloads"

# 7. 检查Maven
echo -e "${BLUE}[7] 检查Maven安装...${NC}"
check_command "mvn" "Maven" "https://maven.apache.org/install.html"

# 8. 检查系统信息
echo -e "${BLUE}[8] 系统信息...${NC}"
echo "操作系统: $(uname -s)"
echo "内核版本: $(uname -r)"
echo "主机名: $(hostname)"
echo "用户名: $(whoami)"
echo "当前目录: $(pwd)"
echo

# 9. 检查内存信息
echo -e "${BLUE}[9] 内存信息...${NC}"
if [ -f /proc/meminfo ]; then
    total_mem=$(grep MemTotal /proc/meminfo | awk '{print $2}')
    mem_gb=$((total_mem / 1024 / 1024))
    echo "总内存: ${mem_gb} GB"
    
    if [ $mem_gb -ge 4 ]; then
        echo -e "${GREEN}✅ 内存充足${NC}"
    else
        echo -e "${YELLOW}⚠️  内存可能不足，建议至少4GB${NC}"
    fi
else
    echo "无法获取内存信息"
fi
echo

# 10. 检查磁盘空间
echo -e "${BLUE}[10] 磁盘空间检查...${NC}"
df -h . | tail -n 1 | awk '{
    print "磁盘: " $1
    print "总空间: " $2
    print "已用: " $3
    print "可用: " $4
    print "使用率: " $5
    
    usage = substr($5, 1, length($5)-1)
    if (usage < 80) {
        print "✅ 磁盘空间充足"
    } else {
        print "⚠️  磁盘空间紧张，建议清理"
    }
}'
echo

# 11. 检查端口占用
echo -e "${BLUE}[11] 检查端口占用...${NC}"
ports=(3306 8080 3000 6379)
for port in "${ports[@]}"; do
    if netstat -tuln 2>/dev/null | grep -q ":$port"; then
        echo -e "${YELLOW}⚠️  端口 $port 已被占用${NC}"
    else
        echo -e "${GREEN}✅ 端口 $port 可用${NC}"
    fi
done
echo

# 12. 检查网络连接
echo -e "${BLUE}[12] 网络连接检查...${NC}"
if ping -c 1 google.com &> /dev/null; then
    echo -e "${GREEN}✅ 网络连接正常${NC}"
else
    echo -e "${YELLOW}⚠️  网络连接可能有问题${NC}"
fi
echo

# 13. 检查项目结构
echo -e "${BLUE}[13] 检查项目结构...${NC}"
directories=("backend" "frontend" "database")
for dir in "${directories[@]}"; do
    if [ -d "$dir" ]; then
        echo -e "${GREEN}✅ $dir 目录存在${NC}"
    else
        echo -e "${RED}❌ $dir 目录不存在${NC}"
    fi
done
echo

# 14. 检查关键文件
echo -e "${BLUE}[14] 检查关键文件...${NC}"
files=("docker-compose.yml" "start.sh" "deploy.sh" "README.md")
for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        echo -e "${GREEN}✅ $file 存在${NC}"
    else
        echo -e "${RED}❌ $file 不存在${NC}"
    fi
done
echo

# 15. 检查Docker服务状态
echo -e "${BLUE}[15] 检查Docker服务状态...${NC}"
if systemctl is-active --quiet docker 2>/dev/null; then
    echo -e "${GREEN}✅ Docker服务运行中${NC}"
elif docker info &> /dev/null; then
    echo -e "${GREEN}✅ Docker运行正常${NC}"
else
    echo -e "${RED}❌ Docker服务未运行${NC}"
    echo -e "${YELLOW}请运行: sudo systemctl start docker${NC}"
fi
echo

# 16. 检查用户权限
echo -e "${BLUE}[16] 检查用户权限...${NC}"
if groups $USER | grep -q docker; then
    echo -e "${GREEN}✅ 用户已加入docker组${NC}"
else
    echo -e "${YELLOW}⚠️  用户未加入docker组${NC}"
    echo -e "${YELLOW}请运行: sudo usermod -aG docker $USER${NC}"
fi
echo

# 总结
echo -e "${GREEN}=====================================${NC}"
echo -e "${GREEN}环境检查完成！${NC}"
echo -e "${GREEN}=====================================${NC}"
echo
echo -e "${BLUE}下一步操作：${NC}"
echo "1. 确保所有必需软件已安装"
echo "2. 运行: ./start.sh 启动系统"
echo "3. 运行: ./quick-test.sh 测试系统"
echo
echo -e "${YELLOW}提示：${NC}"
echo "- 如果Docker权限有问题，请重启系统或重新登录"
echo "- 如需帮助，请查看README.md文件"